<?php

use console\db\channels\TransferTableMigration;
use yii\db\Query;

/**
 * 泰山号资讯转移
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300010_000001_t_cms_article_append extends TransferTableMigration {

    /**
     * @inheritdoc
     */
    public $sourceDb = 'olddb';

    /**
     * @inheritdoc
     */
    public $beforeTruncateTargetTable = false;

    /**
     * @inheritdoc
     */
    public function buildQuery() {
        return (new Query())->from('{{%' . $this->sourceTable . '}}')->where(['>=', 'id', 5397]);
    }

    /**
     * @inheritdoc
     */
    public function safeDown() {
        $this->delete('{{%' . $this->targetTable . '}}', ['>=', 'id', 5398]);
    }

    /**
     * @inheritdoc
     */
    public function validateTransfer(&$data) {
        if (!parent::validateTransfer($data)) {
            return false;
        }
        if ($data['id'] == 5397) {
            $data['id'] = 5400;
        }
        return true;
    }

    /**
     * @inheritdoc
     */
    public function getTransferOptions() {
        return [
            'table' => [
                'cms_article' => 'ts_article',
            ],
            'columns' => [
                'id',
                'channel_id',
                'title',
                'format' => function($data) {
                    if (in_array($data['format'], ['default', 'gallery', 'video'])) {
                        return $data['format'];
                    }
                    return 'default';
                },
                'thumb' => function($data) {
                    if ($data['thumb']) {
                        if (strpos($data['thumb'], 'http') !== false) {
                            return $data['thumb'];
                        }
                        return '/uploads/' . ltrim($data['thumb'], '/');
                    }
                    return '';
                },
                'keywords',
                'description',
                'duration' => function() {
                    return 0;
                },
                'picture_nums' => function() {
                    return 0;
                },
                'author',
                'author_id' => 'bloger_id',
                'created_at' => function($data) {
                    if ($data['pubdate']) {
                        return $data['pubdate'];
                    }
                    if ($data['created_at']) {
                        return $data['created_at'];
                    }
                    return time();
                },
                'updated_at' => function($data) {
                    if ($data['updated_at']) {
                        return $data['updated_at'];
                    }
                    if ($data['created_at']) {
                        return $data['created_at'];
                    }
                    return time();
                },
                'status' => function() {
                    return 10;
                },
            ],
        ];
    }

}
